package com.ruoyi.web.controller.loss;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.loss.domain.bo.WellCustomFieldsBo;
import com.ruoyi.loss.domain.vo.WellCustomFieldsVo;
import com.ruoyi.loss.service.IWellCustomFieldsService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 自定义字段
 *
 * @author yangyong
 * @date 2023-12-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/loss/wellCustomFields")
public class WellCustomFieldsController extends BaseController {

    private final IWellCustomFieldsService iWellCustomFieldsService;

    /**
     * 查询自定义字段列表
     */
    @SaCheckPermission("helper:wellCustomFields:list")
    @GetMapping("/list")
    public TableDataInfo<WellCustomFieldsVo> list(WellCustomFieldsBo bo, PageQuery pageQuery) {
        return iWellCustomFieldsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出自定义字段列表
     */
    @SaCheckPermission("helper:wellCustomFields:export")
    @Log(title = "自定义字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WellCustomFieldsBo bo, HttpServletResponse response) {
        List<WellCustomFieldsVo> list = iWellCustomFieldsService.queryList(bo);
        ExcelUtil.exportExcel(list, "自定义字段", WellCustomFieldsVo.class, response);
    }

    /**
     * 获取自定义字段详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("helper:wellCustomFields:query")
    @GetMapping("/{id}")
    public R<WellCustomFieldsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iWellCustomFieldsService.queryById(id));
    }

    /**
     * 新增自定义字段
     */
    @SaCheckPermission("helper:wellCustomFields:add")
    @Log(title = "自定义字段", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WellCustomFieldsBo bo) {
        return toAjax(iWellCustomFieldsService.insertByBo(bo));
    }

    /**
     * 修改自定义字段
     */
    @SaCheckPermission("helper:wellCustomFields:edit")
    @Log(title = "自定义字段", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WellCustomFieldsBo bo) {
        return toAjax(iWellCustomFieldsService.updateByBo(bo));
    }

    /**
     * 删除自定义字段
     *
     * @param ids 主键串
     */
    @SaCheckPermission("helper:wellCustomFields:remove")
    @Log(title = "自定义字段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iWellCustomFieldsService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
